package vn.com.epi.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Widget
 */
@WebServlet("/form.html")
public class DownloadFileServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadFileServlet() {
        super();
    }
    
    @Override
    public void init() throws ServletException {
        super.init();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String fileName;
        try {
            int day = Integer.parseInt(request.getParameter("day"));
            if (day < 1)
                throw new Exception("Invalid day");
            int month = Integer.parseInt(request.getParameter("month"));
            if (month < 1)
                throw new Exception("Invalid month");            
            int year = Integer.parseInt(request.getParameter("year"));
            if (year < 1)
                throw new Exception("Invalid year");
            fileName = year + "-" + month + "-" + day + ".tar";
            File folder = new File("/data/recommendation/baomoilogs");
            File file = new File(folder, fileName);
            if(!file.exists()){
                throw new ServletException("File " + fileName + " doesn't exists on server.");
            }
            ServletContext context = getServletContext();
            InputStream fis = new FileInputStream(file);
            String mimeType = context.getMimeType(file.getAbsolutePath());
            response.setContentType(mimeType != null ? mimeType:"application/x-tar");
            response.setContentLength((int) file.length());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");            
            ServletOutputStream os = response.getOutputStream();
            byte[] bufferData = new byte[1024];
            int read=0;
            while((read = fis.read(bufferData))!= -1){
                os.write(bufferData, 0, read);
            }
            os.flush();
            os.close();
            fis.close();
            request.setAttribute("respond", "File " + file + " downloaded at client successfully");
        } catch (Exception e) {
            request.setAttribute("respond", e.toString());
        } finally {
            request.getRequestDispatcher("WEB-INF/jsp/respond.jsp").forward(request, response);
        }
    }

}
